const { ccclass, property } = cc._decorator;
declare let eManager,dbh,DBManager,i18n,JsonHttp;
import BaseView from "../../component/BaseView";
import TouFa from "../TouFa";
import YiFu from "../YiFu";
import UIHelper = require("../../component/UIHelper");

@ccclass
export default class HuaZhuangTwoView extends BaseView {

    @property(cc.Node)
    selectNode: cc.Node = null;

    @property(TouFa)
    TouFa: TouFa = null;
    @property(YiFu)
    YiFu: YiFu = null;

    //下一步
    @property(cc.Node)
    nodeNextPoint: cc.Node = null;
    //完成
    @property(cc.Node)
    nodeDone: cc.Node = null;

    @property(cc.Node)
    bgPhoto: cc.Node = null;

    @property(cc.Node)
    btnPhoto: cc.Node = null;

    @property(cc.Animation)
    photoAni: cc.Animation = null;

    //拍照完成 匹配度Ani
    @property(cc.Animation)
    photoDoneAni:cc.Animation = null;
    //匹配度
    @property(cc.Label)
    lblMateNum: cc.Label = null;
    @property(cc.Node)
    btnClose: cc.Node = null;

    _point = 1;
    _isPlay = false;
    _listData = [];
    _isStart = false;//是否是剧情前触发
    onLoad() {
        super.onLoad();
        //@ts-ignore
        this.photoAni.on("finished",this.photoFinish,this);
        eManager.on("MINI_DONE_POINT", this.donePoint, this)
        eManager.on("MINI_DONE_STEP", this.doneStep, this)
        eManager.on("HZ_SHOW_SELECT_LIST", this.showSelectList, this)

        //@ts-ignore
        this._isStart = this.node.openParam;
    };

    onClose(){
    };

    start() {
        super.start();
        this.btnClose.active = !this._isStart;
        this.updatePointShow();
    };

    showSelectList(data:any){
        if(!data)
            return;
        this.selectNode.active = true;
        this._listData = [];
        for (let index = 0; index < 4; index++) {
            this._listData.push(data);
        }
    };

    donePoint(point:number){
        this.nodeNextPoint.active = true;
    };
    doneStep(){
        this.selectNode.active = false;
        this.nodeDone.active = true;
    };

    updatePointShow(){
        this.TouFa.node.active = this._point == 1;
        this.YiFu.node.active = this._point == 2;
        switch (this._point) {
            case 1:
                this.TouFa.initData();
                break;
            case 2:
                this.YiFu.initData();
                break;
        }
        // Initializer.huaZhuangProxy.sendMakeDot(5,this._point,1);
    };

    //下一小阶段
    onClickNextPoint() {
        this.nodeNextPoint.active = false;
        this._point++;
        this.updatePointShow();
    };

    onItemRender(item: any, index: number){
        if(!this._listData)
            return;
        item.listItem.showData(this._listData[index],index);
    };

    onClickDone(){
        this.nodeDone.active = false;
        this.bgPhoto.active = true;
        this.btnPhoto.active = true;
    };

    onClickEnd(isSkip){
        let next = this.params.next && this.params.next.shift();
        if (next) {
            UIHelper.openUI(next, {
                next: this.params.next
            })
            this.node.destroy();
        }
    };

    photoFinish(){
        this.photoDoneAni.node.active = true;
        this.photoDoneAni.play();
        let num = Math.floor(Math.random() * (99 - 80) + 80);
        this.lblMateNum.string = num.toString();
    };

    //拍照
    onClickPhoto(){
        this.photoAni.node.active = true;
        this.photoAni.play();
        this.btnPhoto.active = false;
    };

    onClickSkip(){
        this.onClickEnd(true);
    };

}
